package my.jbf.ALLINONE.DBtest;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import com.example.adapter.PersonAdapter;
//import com.example.domain.Person;
//import com.example.service.PersonService;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import my.jbf.ALLINONE.*;
import android.util.*;
public class DBSQLiteOperateActivity extends Activity
{
	ListView listView;
	PersonService personService;
	OnItemClickListener listViewListener;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.DBmanager);
		PersonService ps = new PersonService(this);
		for (int i=1;i <= 100;i++)
		{
			Person person = new Person();
			person.setName("我是" + i);
			person.setPhone(String.valueOf(Long.parseLong("18888888800") + i));
			ps.save(person);
			Log.i("PersonService", person.toString());
		}
		listViewListener = new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				//得到listView控件
				ListView listView = (ListView)parent;
				//1、如果使用自定义适配器，返回的是Person对象
				//得到该条目数据
//  Person person = (Person)listView.getItemAtPosition(position);
//  //广播出去
//  Toast.makeText(getApplicationContext(), person.toString(), Toast.LENGTH_LONG).show();
				//2、如果使用showList2()方法中的适配器时，则取得的值是不一样的,返回的是cursor
//  Cursor cursor = (Cursor)listView.getItemAtPosition(position);
//  int personid = cursor.getInt(cursor.getColumnIndex("_id"));
//  Toast.makeText(getApplicationContext(), personid+"", Toast.LENGTH_LONG).show();
				//3、如果使用showList()方法中的适配器时，则取得的值是不一样的,返回的是map
				@SuppressWarnings("unchecked")
					Map<String,Object> map = (Map)listView.getItemAtPosition(position);
				String name = map.get("name").toString();
				String personid = map.get("personid").toString();
				Toast.makeText(getApplicationContext(), personid + "-" + name, Toast.LENGTH_LONG).show();
			}
		};
		listView = (ListView) this.findViewById(R.id.listView);
		listView.setOnItemClickListener(listViewListener);
		personService = new PersonService(this);
		showList2();
	}
	private void showList()
	{
		List<Person> persons = personService.getScrollData(0, 50);
		List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
		for (Person person : persons)
		{
			HashMap<String,Object> item = new HashMap<String,Object>();
			item.put("name", person.getName());
			item.put("phone", person.getPhone());
			item.put("amount", person.getAmount());
			item.put("personid", person.getId());
			data.add(item);
		}
		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount});
		listView.setAdapter(adapter);
	}
	public void showList2()
	{
		Cursor cursor = personService.getCursorScrollData(0, 50);
		//该适配器要求返回的结果集cursor必须包含_id字段，所以需要对取得结果集进行处理
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount});
		listView.setAdapter(adapter);
	}
	/**
	 * 自定义适配器
	 */
//	public void showList3()
//	{
//		List<Person> persons = personService.getScrollData(0, 50);
//		/**
//		 * 第一个参数：上下文context，第二个参数：要显示的数据，第三个参数：绑定的条目界面
//		 */
//		PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);
//		listView.setAdapter(adapter);
//	}
}
